package com.ptswitch.material.model.security;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.validation.constraints.Size;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.ColumnDefault;
import org.hxzon.project.SimpleEntity;

@Entity
public class Permission extends SimpleEntity {
    private static final long serialVersionUID = 7187025102792388240L;

//    @ManyToMany(fetch = FetchType.LAZY)
//    //, mappedBy = "permissions")//使用mappedBy则Permission这一端不维护中间表
//    @JoinTable(name = "role_permission", //
//    joinColumns = @JoinColumn(name = "permission_id"), //
//    inverseJoinColumns = @JoinColumn(name = "role_id"))
//    @OrderBy("roleName")
//    private List<Role> roles;

    //assigned
    @Id
    private long id;

    @OneToMany(mappedBy = "permission", fetch = FetchType.LAZY)
    private List<RolePermission> rolePermissions;

    @ColumnDefault(StringDefaultValue)
    @Size(max = SimpleEntity.DefineSize64)
    //级联时处理麻烦，所以不能设为非持久化
    private String permissionName;

    @ColumnDefault(StringDefaultValue)
    @Size(max = SimpleEntity.DefineSize32)
    private String displayName;

    @Transient
    private Permission parent;
    @Transient
    private List<Permission> children;

    //==================
    public Permission() {
    }

    public Permission(String permissionName, String displayName, Permission parent) {
        this.displayName = displayName;
        if (parent != null) {
            this.parent = parent;
            parent.getChildren().add(this);
            this.permissionName = parent.getPermissionName() + "." + permissionName;
        } else {
            this.permissionName = permissionName;
        }
    }

    @Override
    public String toString() {
        return displayName + "[" + permissionName + "]";
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(permissionName).toHashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Permission)) {
            return false;
        }
        Permission other = (Permission) o;
        return new EqualsBuilder().append(this.permissionName, other.permissionName).isEquals();
    }

    //
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Permission getParent() {
        return parent;
    }

    public List<Permission> getChildren() {
        if (children == null) {
            children = new ArrayList<Permission>();
        }
        return children;
    }

    //
    public List<RolePermission> getRolePermissions() {
        return rolePermissions;
    }

    public void setRolePermissions(List<RolePermission> rolePermissions) {
        this.rolePermissions = rolePermissions;
    }

    public String getPermissionName() {
        return permissionName;
    }

    public void setPermissionName(String permissionName) {
        this.permissionName = permissionName;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

}
